<?php
namespace Index\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function Index(){
        // $this->show('login','utf-8');
        layout(false);
        if ($_POST) {
        	$username =I('post.username');
    		
        	// 链接数据库
    		$data =M();
    		// var_dump($data);exit;
    		$sql = "select * from user where username = '$username'";
    		$result =$data->query($sql);
            // var_dump($result);
    		$res =current($result);
    		
    		if (empty($username) or $res['username']!=$username) {
    			// $this->error('用户或密码不正确，请重新输入！');
                $arr =array(
                    'error' =>'0',
                    'msg' =>'用户或密码不正确，请重新输入！'
                    );
                echo json_encode($arr);
    		}else{
                $arr =array(
                    'error' =>'2',
                    'msg' =>'用户验证正确'
                    );
                echo json_encode($arr);
            // $this->success('登陆成功',U('index/Setting/profile'),3);
    		}

        }else{
        	$this->display('login');
        } 

    }


    public function password_ajax()

    {
        if ($_POST) {
            $password =md5(I('post.password'));
            // var_dump($_POST['user']);
            $link =M();
            $sql = "select * from user where username = '{$_POST['user']}'";
            $res = current($link->query($sql));
            if($res['password']!=$password){
                    $arr =array(
                        'error' =>'1',
                        'msg' =>'用户或密码不正确，请重新输入！'
                        );
                    echo json_encode($arr);
            }else{
                $arr =array(
                    'error' =>'2',
                    'msg' =>'用户验证正确'
                    );
                echo json_encode($arr);
                $_SESSION['username']=$res['username'];
                $_SESSION['id']=$res['id'];
                $_SESSION['face']=$res['head_img'];
            }
        }
    }
    public function loginout(){
        session_unset();
        session_destroy();
        $this->success('退出成功',U('index/login/index'),3);
    }
}